function check(name, reg, spanId, okInfo, errorInfo) {
    var flag;
    var val = document.getElementsByName(name)[0].value;
    var oSpanNode = document.getElementById(spanId);
    if (reg.test(val)) {
        oSpanNode.innerHTML = okInfo.fontcolor("green");
        flag = true;
    } else {
        oSpanNode.innerHTML = errorInfo.fontcolor("red");
        flag = false;
    }
    return flag;
}




//alert样式

window.alert = function (str) {
    var shield = document.createElement("DIV");
    shield.id = "shield";
    shield.style.position = "absolute";
    shield.style.left = "0px";
    shield.style.top = "0px";
    shield.style.width = "100%";
    shield.style.height = document.body.scrollHeight + "px";
    //弹出对话框时的背景颜色 
    shield.style.background = "#fff";
    shield.style.textAlign = "center";
    shield.style.zIndex = "25";
    //背景透明 IE有效 
    //shield.style.filter = "alpha(opacity=0)"; 
    var alertFram = document.createElement("DIV");
    alertFram.id = "alertFram";
    alertFram.style.position = "absolute";
    alertFram.style.left = "50%";
    alertFram.style.top = "50%";
    alertFram.style.marginLeft = "-225px";
    alertFram.style.marginTop = "-75px";
    alertFram.style.width = "450px";
    alertFram.style.height = "150px";
    alertFram.style.background = "#ff0000";
    alertFram.style.textAlign = "center";
    alertFram.style.lineHeight = "150px";
    alertFram.style.zIndex = "300";
    strHtml = "<ul style=\"list-style:none;margin:0px;padding:0px;width:100%\">\n";
    strHtml += " <li style=\"background:#DD828D;text-align:left;padding-left:20px;font-size:14px;font-weight:bold;height:25px;line-height:25px;border:1px solid #F9CADE;\">错误消息</li>\n";
    strHtml += " <li style=\"background:#fff;text-align:center;font-size:12px;height:120px;line-height:120px;border-left:1px solid #F9CADE;border-right:1px solid #F9CADE;\">" + str + "</li>\n";
    strHtml += " <li style=\"background:#FDEEF4;text-align:center;font-weight:bold;height:25px;line-height:25px; border:1px solid #F9CADE;\"><input type=\"button\" value=\"确 定\" onclick=\"doOk()\" /></li>\n";
    strHtml += "</ul>\n";
    alertFram.innerHTML = strHtml;
    document.body.appendChild(alertFram);
    document.body.appendChild(shield);
    var ad = setInterval("doAlpha()", 5);
    this.doOk = function () {
        alertFram.style.display = "none";
        shield.style.display = "none";
    }
    alertFram.focus();
    document.body.onselectstart = function () { return false; };
}

//表单总校验
function checkForm() {
    if (checkName() && checkPassword() && ccheckCPassword() && checkStudent_id() && checkTel())
    {
        return true;
    }
    else {
        alert("表单校验不成功，不能提交");
        return false;
        
    }
    
}

function checkName() {
    var reg = new RegExp("^([\u4e00-\u9fa5])+$");
    var flag = check("name", reg, "nameSpan", "", "请输入真实姓名");
    return flag;
}

function checkPassword() {
    var reg = /^.{6,}$/;
    var flag = check("set_pwd", reg, "set_pwdSpan", "", "密码至少为6位！");
    return flag;
}

function checkCPassword() {
    var flag;
    var pass = $("#set_pwd").val();
    var confirmed_pass = $("#commit_pwd").val();
    var oSpanNode = document.getElementById("commit_pwdSpan");
    if (pass != confirmed_pass) {
        oSpanNode.innerHTML = "与密码不一致!".fontcolor("red");
        flag = false;
    }
    else {
        oSpanNode.innerHTML = "Ok".fontcolor("green");
        flag = true;
    }
}

function checkTel() {
    var flag;
        var oSpanNode = document.getElementById("telSpan");
        var isChinaMobile = /^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|18[2-478])\d{8}$/; //移动方面最新答复
        var isChinaUnion = /^(?:13[0-2]|145|15[56]|176|18[56])\d{8}$/; //向联通微博确认并未回复
        var isChinaTelcom = /^(?:133|153|177|18[019])\d{8}$/; //1349号段 电信方面没给出答复，视作不存在
        var isOtherTelphone = /^170([059])\d{7}$/;//其他运营商
        var tel_value = $("#tel").val();
        if (isChinaMobile.test(tel_value))
        {
            oSpanNode.innerHTML = "中国移动通信".fontcolor("blue");
            flag = true;
        }
        if (isChinaUnion.test(tel_value))
        {
            oSpanNode.innerHTML = "中国联通".fontcolor("orange");
            flag = true;
        }
        if(isChinaTelcom.test(tel_value))
        {
            oSpanNode.innerHTML = "中国电信".fontcolor("#68228B");
            flag = true;
        }
        if(isOtherTelphone.test(tel_value))
        {
            oSpanNode.innerHTML = "其它运营商".fontcolor("#A52A2A");
            flag = true;
        }
        if(!isChinaMobile.test(tel_value)&&!isChinaUnion.test(tel_value)&&!isChinaTelcom.test(tel_value)&&!isOtherTelphone.test(tel_value))
        {
            oSpanNode.innerHTML = "请输入合法的手机号".fontcolor("red");
            flag = false;
        }
        return flag;
    }



function checkStudent_id() {
    var academy_name = "";
    var oSpanNode = document.getElementById("student_idSpan");
    var reg = new RegExp("201[3-7][0-2][0-9][0-9][0-9][0-3][0-9]");
    var flag = check("student_id", reg, "student_idSpan", "", "请输入正确学号");
    if (flag) {
        var str = $("#student_id").val();
        //alert(str.charAt(4)+str.charAt(5));
        var academy_code = str.charAt(4) + str.charAt(5);
        switch (academy_code) {
            case "01": academy_name = "船舶工程学院";
                break;
            case "02": academy_name = "航天与建筑工程学院";
                break;
            case "03": academy_name = "动力与能源工程学院";
                break;
            case "04": academy_name = "自动化学院";
                break;
            case "05": academy_name = "水声工程学院";
                break;
            case "06": academy_name = "计算机学院~";
                break;
            case "07": academy_name = "机电工程学院";
                break;
            case "08": academy_name = "信息与通信工程学院";
                break;
            case "09": academy_name = "经济管理学院";
                break;
            case "10": academy_name = "材料科学与化学工程学院";
                break;
            case "11": academy_name = "理学院";
                break;
            case "12": academy_name = "外语系";
                break;
            case "13": academy_name = "人文社会科学学院";
                break;
            case "14": academy_name = "马克思主义学院";
                break;
            case "15": academy_name = "核科学与技术学院";
                break;
            case "16": academy_name = "体育部";
                break;
            case "20": academy_name = "软件学院//^_^";
                break;
            case "21": academy_name = "国家保密学院~";
                break;
            default: academy_name = "请填写";
        }
        $("#student_dept").val(academy_name);
        $.ajax({
            type: "POST",
            url: "/Register/check",
            data: { student_id: str },
            dataType: "json",
            success: function (request_val) {
                if (request_val == "0") {
                    oSpanNode.innerHTML = "该学号可以注册！".fontcolor("green");
                }
                else {
                    oSpanNode.innerHTML = "该学号已注册，非本人操作联系管理员".fontcolor("red");
                }
            },
            error: function () {
                alert("error");
            }
        });
    }
    return flag;
}